package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.jersey.json.impl.provider.entity.JSONArrayProvider;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.ApUserSearch;
import com.usian.model.search.UserSearchDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.client.ApBehaviorEntryClient;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;

/**
 * ClassName: ApUserSearchServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/10-16:32
 * Version: 1.0
 * Description:
 */
@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private ApBehaviorEntryClient apBehaviorEntryClient;

    /**
     * 添加app用户搜索信息表
     * @return
     */
    @Override
    @Async("taskExecutor")
    public void saveApUserSearch(Integer id, String words) {
        //根据entry_id 和 keyword查询当前数据是否存在
        ApUserSearch userSearch = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, id).eq(ApUserSearch::getKeyword, words));

        //判断状态是否存在
        if (userSearch!=null&&userSearch.getStatus()==1){
            return;
        }else if (userSearch!=null&&userSearch.getStatus()==0){
            userSearch.setStatus(1);
            updateById(userSearch);
            return;
        }
        //添加数据
        ApUserSearch apUserSearch = new ApUserSearch();
        apUserSearch.setCreatedTime(new Date());
        apUserSearch.setEntryId(id);
        apUserSearch.setKeyword(words);
        apUserSearch.setStatus(1);
        save(apUserSearch);

    }
    /**
     * 搜索记录全查
     * @return
     */
    @Override
    public ResponseResult findApUserSearch(UserSearchDto dto) {
        //校验参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //拿到用户的id
        ApUser user = AppThreadLocalUtils.getUser();
        //判断当前用户是否登录
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryClient.showBehavior(user.getId(), dto.getEquipmentId());

        Page page = new Page(dto.getPageNum(),dto.getPageSize());

        //判断当前用户是否登录
        if (apBehaviorEntry.getType()==1){

            LambdaQueryWrapper<ApUserSearch> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserSearch::getEntryId,user.getId());
            queryWrapper.orderByDesc(ApUserSearch::getCreatedTime);
            IPage pageInfo = page(page, queryWrapper);
            ResponseResult responseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(),(int)pageInfo.getTotal());
            responseResult.setData(pageInfo.getRecords());
            return responseResult;

        }else {

            LambdaQueryWrapper<ApUserSearch> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserSearch::getEntryId,dto.getEquipmentId());
            queryWrapper.orderByDesc(ApUserSearch::getCreatedTime);
            IPage pageInfo = page(page, queryWrapper);
            ResponseResult responseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(),(int)pageInfo.getTotal());
            responseResult.setData(pageInfo.getRecords());
            return responseResult;

        }
    }
    /**
     * 搜索记录删除
     * @return
     */
    @Override
    public ResponseResult updateStatus(Integer id) {
        //校验参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断数据是否存在
        ApUserSearch apUserSearch = getById(id);
        if (apUserSearch==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //修改参数
        update(Wrappers.<ApUserSearch>lambdaUpdate().set(ApUserSearch::getStatus,0).eq(ApUserSearch::getId,id));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
